<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    ></van-nav-bar>

    <input type="file" hidden ref="file" @change="onFileChange" />
    <!-- 个人信息 -->
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      @click="isUpdataNameShow = true"
      :value="user.name"
      is-link
    ></van-cell>
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      @click="isUpdataGenderShow = true"
      is-link
    ></van-cell>
    <van-cell
      title="生日"
      @click="isUpdataBirthdayShow = true"
      :value="user.birthday"
      is-link
    ></van-cell>

    <!-- 编辑昵称 -->
    <van-popup
      v-model="isUpdataNameShow"
      position="bottom"
      style="height: 100%"
    >
      <update-name
        v-if="isUpdataNameShow"
        @close="isUpdataNameShow = false"
        v-model="user.name"
      ></update-name>
    </van-popup>

    <!-- 编辑性别 -->
    <van-popup v-model="isUpdataGenderShow" position="bottom">
      <update-gender
        v-if="isUpdataGenderShow"
        v-model="user.gender"
        @close="isUpdataGenderShow = false"
      ></update-gender>
    </van-popup>

    <!-- 编辑生日 -->
    <van-popup v-model="isUpdataBirthdayShow" position="bottom">
      <update-birthday
        v-if="isUpdataBirthdayShow"
        v-model="user.birthday"
        @close="isUpdataBirthdayShow = false"
      ></update-birthday>
    </van-popup>

    <!-- 编辑头像 -->
    <van-popup
      v-model="isUpdataPhotoShow"
      style="height: 100%"
      position="bottom"
    >
      <update-photo
        v-if="isUpdataPhotoShow"
        @close="isUpdataPhotoShow = false"
        @updata-photo="user.photo = $event"
        :img="img"
      ></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdatePhoto from './components/update-photo'

export default {
  name: 'UserProfile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto,
  },
  props: {},
  data() {
    return {
      // 用户的个人信息
      user: {},
      // 控制修改个人资料组件的显示与隐藏
      isUpdataNameShow: false,
      isUpdataGenderShow: false,
      isUpdataBirthdayShow: false,
      isUpdataPhotoShow: false,
      // 预览的图片
      img: null,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.loadUserProfile()
  },
  mounted() {},
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile()
        // console.log(data)
        this.user = data.data
      } catch (err) {
        this.$toast('数据获取失败')
      }
    },

    onFileChange() {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文章对象获取bolo 数据
      const data = window.URL.createObjectURL(file)
      this.img = data
      // console.log(data)
      this.isUpdataPhotoShow = true
      // 每次关闭弹层的时候把value 值清空 解决选择同一种图片时打不开弹层的问题
      this.$refs.file.value = ''
    },
  },
}
</script>
<style lang='less' scoped>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }

  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>
